<template>
  <div class="markdown">
    <a-form style="width: calc(100% - 80px)">
      <a-form-item
        style="margin-top: 10px"
        label="标题"
        v-bind="validateInfos.title"
      >
        <a-input placeholder="Please input title" v-model:value="from.title" />
      </a-form-item>
      <a-form-item label="&ensp;&ensp;标签">
        <template v-for="tag in state.tags" :key="tag">
          <a-tooltip v-if="tag.length > 20" :title="tag">
            <a-tag :closable="true" @close="handleClose(tag)">
              {{ `${tag.slice(0, 20)}...` }}
            </a-tag>
          </a-tooltip>
          <a-tag v-else :closable="true" @close="handleClose(tag)">
            {{ tag }}
          </a-tag>
        </template>
        <a-input
          v-if="state.inputVisible"
          ref="inputRef"
          v-model:value="state.inputValue"
          type="text"
          size="small"
          :style="{ width: '78px' }"
          @blur="handleInputConfirm"
          @keyup.enter="handleInputConfirm"
        />
        <a-tag
          v-else
          style="background: #fff; border-style: dashed"
          @click="showInput"
        >
          <plus-outlined />
          new Tag
        </a-tag>
      </a-form-item>
    </a-form>

    <v-md-editor
      v-model="content"
      left-toolbar="undo redo clear | h bold italic strikethrough quote | ul ol table hr | link image code"
      height="calc(100vh - 250px)"
    ></v-md-editor>
      <a-button sy type="primary" @click.prevent="onSubmit">Create</a-button>
  </div>
</template>

<script setup lang="ts">
import { nextTick, onBeforeMount, reactive, ref, toRaw } from "vue";
import { PlusOutlined } from "@ant-design/icons-vue";
import { Form, message } from "ant-design-vue";
import myAxios from "@/plugins/myAxios";
import router from "@/router";
import { useRoute } from "vue-router";

const useForm = Form.useForm;

let from = reactive({
  title: undefined,
});
let content = ref("");
const rulesRef = reactive({
  title: [
    {
      required: true,
      message: "Please input post name",
    },
  ],
  content: [
    {
      required: true,
      message: "Please input post name",
    },
  ],
});

const { validate, validateInfos } = useForm(from, rulesRef);
const inputRef = ref();
const state = reactive({
  tags: [],
  inputVisible: false,
  inputValue: "",
});
const handleClose = (removedTag: string) => {
  const tags = state.tags.filter((tag) => tag !== removedTag);
  state.tags = tags;
};
const showInput = () => {
  state.inputVisible = true;
  nextTick(() => {
    inputRef.value.focus();
  });
};
const handleInputConfirm = () => {
  const inputValue: string = state.inputValue;
  let tags: never[] = state.tags;
  if (inputValue && tags.indexOf(inputValue) === -1) {
    tags = [...tags, inputValue];
  }
  Object.assign(state, {
    tags,
    inputVisible: false,
    inputValue: "",
  });
};
const onSubmit = () => {
  validate()
    .then(() => {
      loadTypeData();
    })
    .catch(() => {
      if (from.title == "")
        message.warning("请将标题补充完整");
    });
};
const loadTypeData = () => {
  const id = route.params.category;
  const Params = {
    title : from.title,
    id : id,
    content: content.value,
    tags: state.tags,
  };
  myAxios.post(`/post/edit`,Params).then((res: any) => {
    // console.log(res);
    if(res == true){
      message.success("文章修改成功")
      router.push(`/submitSuccess/${id}`);
    }else {
      message.error("系统错误")
    }
  });
};
const route = useRoute()
const getPostById = async () => {
  const id = route.params.category;
  await myAxios.get(`post/get/vo?id=${id}`).then((res: any) => {
    // console.log(res);
    if (res !== null) {
      from.title = res.title;
      content.value = res.content;
      state.tags = res.tagList;
      // console.log(from);
    }else {
      from = res;
      router.push("/404")
    }
  });
};

onBeforeMount(()=>{
  getPostById()
})
</script>

<style scoped lang="less">
.markdown {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  .submitButton {
    width: 90%;
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
    .ant-btn-primary {
      width: 100px;
    }
  }
}
</style>